﻿using System;
using System.Collections.Generic;
using System.Xml;

namespace Yawf.Web.Managers.Entities {
	public class MethodData {
		private String methodName;
		private String className;
		private String resultName;
		private String springBean;
		private IList<ParameterData> parameters;

		// Properties
		public String MethodName { get { return methodName;} set {methodName = value;} }
		public String ClassName { get { return className; } set { className = value; } }
		public String ResultName { get { return resultName; } set { resultName = value; } }
		public String SpringBean { get { return springBean; } set { springBean = value; } }
		public IList<ParameterData> Parameters { get { return parameters; } set { parameters = value; } }

		/// <summary>
		/// Constructor with Xml Element, reads method-invoke tag
		/// into properties
		/// </summary>
		/// <param name="elem">XmlElement of method-invoke tag</param>
		public MethodData(XmlElement elem) {
			XmlNodeList paramNodes;
			XmlNode node;
			String tmpStr;

			// Read name of method to execute
			MethodName = elem.GetAttribute("method-name");
			
			// Read name of result
			ResultName = elem.GetAttribute("result-name");

			// Read name of Spring object to use
			tmpStr = elem.GetAttribute("spring-bean");
			if (tmpStr != null && tmpStr.Trim() != "") {
				SpringBean = tmpStr;
			}

			// Read class name if it exists
			tmpStr = elem.GetAttribute("class-name");
			if (tmpStr != null && tmpStr.Trim() != "") {
				ClassName = tmpStr;
			}

			// Add parameters
			paramNodes = elem.SelectNodes("parameters/parameter");
			Parameters = new List<ParameterData>();
			for (int i = 0; paramNodes != null && i < paramNodes.Count; i++) {
				node = paramNodes[i];
				if (node is XmlElement){
					Parameters.Add(new ParameterData((XmlElement)node));
				}
			}
		}					// MethodData.C
	}
}
